clear all
cap close
set more off

cd ".\Data_Program"

use county,clear

* merge with division data
merge m:1 county using Econdata\countytodivision
keep if _merge==3 
drop _merge

* merge with neighbor data
merge 1:1 quarter county using distance_10nb.dta
keep if _merge==3
drop _merge

* merge with Mintel data (proprietary)
merge 1:1 county quarter using Proprietary\mintel
drop if _merge==2 
drop _merge 
replace finad=0 if finad==.
replace nonfinad=0 if nonfinad==.
replace ad=0 if ad==.
drop ad nonfinad

* merge with county's own economic data

    * CoreLogic house price growth (proprietary)
merge 1:1 quarter county using Proprietary\hpi_county_quarter_exp
drop if _merge==2
drop _merge
replace hpgr=hpgr4
drop qhpi hpgr1 hpgr4

    * Census Buearu population growth
gen year=yofd(dofq(quarter))	
merge m:1 year county using Econdata\pop_county
drop if _merge==2
drop _merge
drop fracm frach

    * QCEW employment growth
merge m:1 year county using Econdata\emp_county_year
drop if _merge==2
drop _merge 

    * CCP subprime share
merge 1:1 quarter county using Proprietary\subprime_county_quarter
drop if _merge==2  
drop _merge 

     * Census education level in 2010
merge m:1 county using Econdata\edu
drop if _merge==2  
drop _merge 

* demographic X year controls
tab year, gen(yy)
foreach i of numlist 1/10{
   gen baboveyy`i' = babove*yy`i'
   gen fracbyy`i' = fracb*yy`i'
   gen fracyyy`i' = fracy*yy`i'

foreach b of numlist 1/10{ 
   gen babove2yy`i'`b' = babove2`b'*yy`i'
}
}

* IRS migration flows
merge m:1 year county using Econdata\migration
drop if _merge==2  
drop _merge

* Regressions
xtset county quarter

* weights
sort county quarter
by  county: egen w1 = mean(tot)
by  county: egen w2 = mean(pop)
gen w=w1


  * outcome var--change in finshare
foreach x of varlist finshare finshare_refi finshare_pur finshare2*{
gen d`x'=`x'-l4.`x'
}

gen dfinad = (log(finad+1)-log(l4.finad+1))/100
foreach b of numlist 1/10{ 
   gen dfinad2`b' = (log(finad2`b'+1)-log(l4.finad2`b'+1))/100
}


*===============================================================================
* Regression
*===============================================================================

tempname dis_all
postfile `dis_all' b b_dis_all se_dis_all using dis_all.dta,replace

tempname dis_refi
postfile `dis_refi' b b_dis_refi se_dis_refi using dis_refi.dta,replace

tempname dis_pur
postfile `dis_pur' b b_dis_pur se_dis_pur using dis_pur.dta,replace

forval b= 1/10{
    
local varlist="dfinshare2 hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 babove2yy10"
foreach x of local varlist{
gen `x'=`x'`b'
}

local ctrl "hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 linflow loutflow"

reghdfe dfinshare dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `dis_all' (`b') (_b[dfinshare2]) (_se[dfinshare2])

reghdfe dfinshare_refi dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `dis_refi' (`b') (_b[dfinshare2]) (_se[dfinshare2])

reghdfe dfinshare_pur dfinshare2 `ctrl' [aw=w],a(county division#quarter) cluster(county)
post `dis_pur' (`b') (_b[dfinshare2]) (_se[dfinshare2])

drop dfinshare2 hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 dfinad2 babove2yy1 babove2yy2 babove2yy3 babove2yy4 babove2yy5 babove2yy6 babove2yy7 babove2yy8 babove2yy9 babove2yy10
}
postclose `dis_all'
postclose `dis_refi'
postclose `dis_pur'
